
package com.yushu.common.util.date;
import org.joda.time.DateTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


 /**
 * 日期时间处理工具类
 * @author test
 * @CreateDate 2020-12-03 15:47:24
 */
public class DateUtils {


    public static Date stringToDate(String dateString) throws ParseException {

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd HH");
        SimpleDateFormat format4 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format5 = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat format6 = new SimpleDateFormat("yyyy");
        String date = dateString;
        Date dateNew;


        if (date.length() == 19) {
            dateNew = format1.parse(date);
        } else if (date.length() == 16) {
            dateNew = format2.parse(date);

        } else if (date.length() == 13) {
            dateNew = format3.parse(date);

        } else if (date.length() == 10) {
            dateNew = format4.parse(date);

        } else if (date.length() == 7) {
            dateNew = format5.parse(date);

        } else if (date.length() == 4) {
            dateNew = format6.parse(date);

        } else if (date.length() == 0) {
            dateNew = null;

        } else {
            dateNew = format1.parse(date);
        }

        return dateNew;

    }


    public static String dateToString(Date date) {
        DateTime dt = new DateTime(date);
        String dateString = dt.toString("yyyy-MM-dd HH:mm:ss");
        if (dateString.substring(4, 19).equals("00-00 00:00:00")) {
            dateString = dateString.substring(0, 4);
        } else if (dateString.substring(7, 19).equals("-00 00:00:00")) {
            dateString = dateString.substring(0, 7);
        } else if (dateString.substring(10, 19).equals(" 00:00:00")) {
            dateString = dateString.substring(0, 10);
        } else if (dateString.substring(13, 19).equals(":00:00")) {
            dateString = dateString.substring(0, 13);
        } else if (dateString.substring(16, 19).equals(":00")) {
            dateString = dateString.substring(0, 16);
        }
        return dateString;
    }

    public static String dateToString() {
        Date date = new Date();
        DateTime dt = new DateTime(date);
        String dateString = dt.toString("yyyy-MM-dd HH:mm:ss");
        if (dateString.substring(4, 19).equals("00-00 00:00:00")) {
            dateString = dateString.substring(0, 4);
        } else if (dateString.substring(7, 19).equals("-00 00:00:00")) {
            dateString = dateString.substring(0, 7);
        } else if (dateString.substring(10, 19).equals(" 00:00:00")) {
            dateString = dateString.substring(0, 10);
        } else if (dateString.substring(13, 19).equals(":00:00")) {
            dateString = dateString.substring(0, 13);
        } else if (dateString.substring(16, 19).equals(":00")) {
            dateString = dateString.substring(0, 16);
        }
        return dateString;
    }


}
